package com.wrb.coupon.redis;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

import java.util.HashSet;
import java.util.Set;

@Slf4j
@Configuration
public class JedisClientClusterConfig {

	@Value("${spring.redis.cache.clusterNodes}")
	private String clusterNodes;

	@Value("${spring.redis.cache.commandTimeout}")
	private int commandTimeout;

	@Bean
	public JedisCluster getJedisCluster() {
		String[] serverArray = clusterNodes.split(",");
		Set<HostAndPort> nodes = new HashSet<>();
		for (String ipPort : serverArray) {
			String[] ipPortPair = ipPort.split(":");
			nodes.add(new HostAndPort(ipPortPair[0].trim(), Integer.valueOf(ipPortPair[1].trim())));
		}
		JedisCluster jedisCluster = new JedisCluster(nodes, commandTimeout);
		log.info("jedisCluster注入成功！！");
		return jedisCluster;
	}

}